/*
 * Copyright © 2011 Nokia Corporation. All rights reserved.
 * Nokia and Nokia Connecting People are registered trademarks of Nokia Corporation. 
 * Oracle and Java are trademarks or registered trademarks of Oracle and/or its
 * affiliates. Other product and company names mentioned herein may be trademarks
 * or trade names of their respective owners. 
 * See LICENSE.TXT for license information.
 */ 

package com.nokia.example.wordpress.helpers;

import java.io.IOException;
import java.io.InputStream;
import java.util.Hashtable;
import javax.microedition.lcdui.Image;

/**
 * Utility for loading image resources.
 */
public class ImageLoader {

    private static ImageLoader self;
    private Hashtable cache = new Hashtable();

    private ImageLoader() {
    }

    public static ImageLoader getInstance() {
        if (self == null) {
            self = new ImageLoader();
        }
        return self;
    }

    /**
     * Loads an image from resources and returns it.
     * Caches all loaded images in hopes of saving some memory.
     * @param imagePath
     * @return
     * @throws IOException
     */
    public Image loadImage(String imagePath) throws IOException {

        Image image = (Image) cache.get(imagePath);
        if (image == null) {
            image = Image.createImage(imagePath);
            cache.put(imagePath, image);
        }
        return image;
    }
}
